package staticPage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureManagedBean;
import menus.LectureMenuItem;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="staticPagesBean")
@RequestScoped
public class StaticPagesManagedBean {
    
    private String newPageName;

    public StaticPagesManagedBean() {        
    }
    
    public String getNewPageName() {
        return newPageName;
    }

    public void setNewPageName(String newPageName) {
        this.newPageName = newPageName;
    } 
    
    public void createPage() {
        StaticPage page = new StaticPage();
        page.setName(this.newPageName);
        FacesContext context = FacesContext.getCurrentInstance();
        LectureManagedBean lb = (LectureManagedBean) context.getApplication().evaluateExpressionGet(context, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        lecture.addStaticPage(page);
        lecture.getMenuItems().add(new LectureMenuItem(this.newPageName, 
                                                        "staticPage.jsf?staticPageName="+this.newPageName));
    }
    
    public void removeStaticPage(StaticPage page) {
        FacesContext context = FacesContext.getCurrentInstance();
        LectureManagedBean lb = (LectureManagedBean) context.getApplication().evaluateExpressionGet(context, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        lecture.removeStaticPage(page);
        for (LectureMenuItem l: lecture.getMenuItems()) {
            if (l.getName().equals(page.getName())) {
                lecture.getMenuItems().remove(l);
            }
        }
    }
}
